﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Objects;
using BAL;

public partial class Admin_UCControls_UCNguoiDung_Update : System.Web.UI.UserControl
{
    public static NguoiDung ItemNguoiDung;

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            ItemNguoiDung = new NguoiDung();
            ItemNguoiDung = (NguoiDung)Session["ItemNguoiDung"];

            if (ItemNguoiDung != null)
            {
                txtTenNguoiDung.Text = ItemNguoiDung.TenNguoiDung;
                txtTenDangNhap.Text = ItemNguoiDung.TenDangNhap;
                ddlquyen.SelectedIndex = ItemNguoiDung.Quyen;
            }
        }
    }

    protected void btnLuu_Click(object sender, EventArgs e)
    {
        int item = 0;

        if (ItemNguoiDung == null)
        {
            item = BALNguoiDung.Insert_NguoiDung(txtTenNguoiDung.Text, txtTenDangNhap.Text, Common.ConvertMD5.EncryptionPassword(txtMatKhau.Text), DateTime.Now, DateTime.Now, Convert.ToInt16(ddlquyen.SelectedValue), 0);

        }
        else
        {
            item = BALNguoiDung.Update_NguoiDung(Int32.Parse(ItemNguoiDung.Id.ToString()), txtTenNguoiDung.Text, txtTenDangNhap.Text, Common.ConvertMD5.EncryptionPassword(txtMatKhau.Text), ItemNguoiDung.NgayTao, ItemNguoiDung.LanDangNhapCuoi, Convert.ToInt16(ddlquyen.SelectedValue), 0);

        }

        if (item > 0)
        {
            lblThongbao.Visible = true;
            lblThongbao.Text = "Cập nhật thành công";

        }
        else
        {
            lblThongbao.Visible = true;
            lblThongbao.Text = "Cập nhật thất bại";
        }
    }

    protected void btnQuaylai_Click(object sender, EventArgs e)
    {
        Response.Redirect("../Admin/NguoiDung.aspx");
    }
}
